package com.lagou.service;

import org.apache.dubbo.config.annotation.Service;
import org.apache.dubbo.rpc.RpcContext;

import java.util.concurrent.ThreadLocalRandom;

@Service
public class HelloServiceImpl implements HelloService {

    @Override
    public String hello() {
        String ip = RpcContext.getContext().getAttachment("ip");
        System.out.println("请求ip:"+ip);
        return "hellp word";
    }


    @Override
    public String methodA() {
        sleep();
        return "this is methodA";
    }

    @Override
    public String methodB() {
        sleep();
        return "this is methodB";
    }

    @Override
    public String methodC() {
        sleep();
        return "this is methodC";
    }

    private void sleep() {
        try {
            Thread.sleep(getRandomInt(300));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    private int getRandomInt(int random) {
        return ThreadLocalRandom.current().nextInt(random);
    }
}
